package com.rpc.core.holder.bean;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ExportBean implements Bean{
    private Object bean;

    private String beanName;

    private Map<String, Method> methodMap=new ConcurrentHashMap<>();


    public ExportBean(Object bean,String name) {
        this.bean = bean;
        this.beanName=name;
        build();
    }


    private void build() {
        Class<?> beanClass = this.bean.getClass();

        for (Method method : beanClass.getMethods()) {
            String key = method.getName();
            methodMap.put(key,method);
        }

    }


    // method + args
    public Object invoke(String methodName, Object[] args) throws Throwable{

        Method method = this.methodMap.get(methodName);

        try {
            return method.invoke(this.bean,args);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }





}
